﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class findAllorOneStudentScoreInfo : Form
    {
        public findAllorOneStudentScoreInfo()
        {
            InitializeComponent();
        }
        User user;

        internal User User
        {
            get { return user; }
            set { user = value; }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            string sql = "select * from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username+"'))";

            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value.ToString())==0)
                {   
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void findstuno_Click(object sender, EventArgs e)
        {
            string stuno = txt_stuno.Text;
            if (string.IsNullOrEmpty(stuno))
            {
                MessageBox.Show("查询不能为空");
                return;
            }
            string sql = "select * from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username + "'))"+" and stuno=" + stuno;
            DataTable dt=DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void btn_findcno_Click(object sender, EventArgs e)
        {
            string cno = txt_cno.Text;
            if (string.IsNullOrEmpty(cno))
            {
                MessageBox.Show("查询不能为空");
                return;
            }
            string sql = "select * from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username + "'))"+" and cno=" + cno;
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }     
        private void update_Click(object sender, EventArgs e)
        {
           
        }
        
        private void findAllorOneStudentScoreInfo_Load(object sender, EventArgs e)
        {
            string sql = "select * from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username + "'))";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value.ToString()) == 0)
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                }
            }
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;   
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //string strcolumn = dataGridView1.Columns[e.ColumnIndex].HeaderText;//获取列标题
            //string strrow = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();//获取焦点触发行的第一个值
            //string value = dataGridView1.CurrentCell.Value.ToString();//获取当前点击的活动单元格的值
            //string strcomm = "update grade set " + strcolumn + "='" + value + "'where id = " + strrow;
            ////update FilTer set 列名 = value where id = 3
            //DBAccess.GetState(strcomm);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //string stuno = txt_stuno.Text;
            //if(stuno==""){
            //    MessageBox.Show("请输入学号进行删除");
            //}
            //string sql = "delete from grade where stuno="+stuno;
            //int res = DBAccess.GetState(sql);
            //if(res!=0){
            //    MessageBox.Show("删除成功!");

            //}else{
            //    MessageBox.Show("删除失败");
            //}
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //string cno = txt_cno.Text;
            //if (cno == "")
            //{
            //    MessageBox.Show("请输入学号进行删除");
            //}
            //string sql = "select stuno as '学号',g.cno as '课程号',score as '分数' from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username + "'))" + " and cno=" + cno;
            //int res = DBAccess.GetState(sql);
            //if (res != 0)
            //{
            //    MessageBox.Show("删除成功!");
            //}
            //else
            //{
            //    MessageBox.Show("删除失败");
            //}
        }

        private void btn_findtwocondiction_Click(object sender, EventArgs e)
        {
            string cno = txt_cno.Text;
            string stuno = txt_stuno.Text;
            if (cno == "" || stuno == "")
            {
                MessageBox.Show("请输入学号和课程号进行查看");
            }
            string sql = "select * from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username + "'))" + " and stuno=@stuno and cno = @cno ";
            SqlParameter[] sqlarray = { new SqlParameter("@stuno", stuno), new SqlParameter("@cno", cno) };
            DataTable dt=DBAccess.GetTable(sql,sqlarray);
            dataGridView1.DataSource = dt;
        }

        private void btn_deltwocondiction_Click(object sender, EventArgs e)
        {
            string cno = txt_cno.Text;
            string stuno = txt_stuno.Text;
            if (cno == "" || stuno == "")
            {
                MessageBox.Show("请输入学号和课程号进行删除");
            }
            string sql = "select * from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username + "'))" + " and stuno=@stuno and cno = @cno ";
            SqlParameter[] sqlarray = { new SqlParameter("@stuno", stuno), new SqlParameter("@cno", cno) };
            int res = DBAccess.GetState(sql);
            if (res != 0)
            {
                MessageBox.Show("删除成功!");
                button3_Click(sender, e);
            }
            else
            {
                MessageBox.Show("查无该信息");
            }
            
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string stuno = txt_stuno.Text;
            if (string.IsNullOrEmpty(stuno))
            {
                MessageBox.Show("查询不能为空");
                return;
            }
            string sql = "select * from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username + "'))" + " and stuno like '%" + stuno + "%'";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            string cno = txt_cno.Text;
            if (string.IsNullOrEmpty(cno))
            {
                MessageBox.Show("查询不能为空");
                return;
            }
            string sql = "select * from grade g where stuno in (select stuno from student where classno in (select classno from teach  where teach.tno='" + user.Username + "'))" + " and cno like '%" + cno + "%'";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col = dataGridView1.CurrentCellAddress.X + 1;
            string colo = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].HeaderText;
            string value = dataGridView1.Rows[row - 1].Cells[col - 1].Value.ToString();
            int row0 = Convert.ToInt32(dataGridView1.Rows[row - 1].Cells[0].Value);
            int row1 = Convert.ToInt32(dataGridView1.Rows[row - 1].Cells[1].Value);
            string sql = "update grade set " + colo + "=@value where stuno=@id and cno=@id1 ";
            SqlParameter[] pArr = { new SqlParameter("@value", value), new SqlParameter("@id", row0),
                                  new SqlParameter("@id1", row1)};
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[2].ReadOnly = true;
            
            int res = DBAccess.GetState(sql, pArr);
            if (res != 0)
            {
                MessageBox.Show("修改成功");
                findAllorOneStudentScoreInfo_Load(sender, e);
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col = dataGridView1.CurrentCellAddress.X + 1;
            txt_stuno.Text = dataGridView1.Rows[row - 1].Cells[0].Value.ToString();
            txt_cno.Text = dataGridView1.Rows[row - 1].Cells[1].Value.ToString();
        }
    }
}
